﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFSTools.TFSLibrary
{
    public sealed class TFSWorkItemType : TFSServerEntity
    {
        private WorkItemType serverWorkItemType;
        private TFSWorkItemTransitionCollection transitions = null;
        private TFSFieldDefinitionCollection fieldDefinitions;

        internal TFSWorkItemType(ITFSConnection connection, WorkItemType workItemType)
            : base(connection)
        {
            this.serverWorkItemType = workItemType;
        }

        internal WorkItemType ServerEntity { get { return this.serverWorkItemType; } }

        public string Name { get { return this.serverWorkItemType.Name; } }

        public string Description { get { return this.serverWorkItemType.Description; } }

        public TFSWorkItemTransitionCollection GetTransitions()
        {
            if (this.transitions == null)
            {
                this.transitions = TFSWorkItemTransitionCollection.Create(this);
            }

            return this.transitions;
        }

        public TFSWorkItemTransitionCollection GetTransitions(string state)
        {
            return new TFSWorkItemTransitionCollection(this.GetTransitions().Where(x => x.From == state).ToList(), this);
        }

        public TFSWorkItemTransition GetTransition(string from, string to)
        {
            return this.GetTransitions().FirstOrDefault(transition => transition.From == from && transition.To == to);
        }

        public TFSFieldDefinitionCollection FieldDefinitions
        {
            get
            {
                if (this.fieldDefinitions == null)
                {
                    this.fieldDefinitions = TFSFieldDefinitionCollection.Create(this.Connection, this.serverWorkItemType.FieldDefinitions);
                }

                return this.fieldDefinitions;
            }
        }

        public override string ToString()
        {
            return this.Name;
        }
    }

    public sealed class TFSWorkItemTypeCollection : TFSServerEntityCollection<TFSWorkItemType>
    {
        public TFSWorkItemTypeCollection(ITFSConnection connection)
            : base(connection)
        {
        }

        public TFSWorkItemTypeCollection(ITFSConnection connection, IList<TFSWorkItemType> list)
            : base(connection, list)
        {
        }

        public TFSWorkItemTypeCollection(ITFSConnection connection, int capacity)
            : base(connection, capacity)
        {
        }

        internal static TFSWorkItemTypeCollection Create(ITFSConnection connection, WorkItemTypeCollection collection)
        {
            var list = new List<TFSWorkItemType>();

            foreach (WorkItemType workItemType in collection)
            {
                list.Add(new TFSWorkItemType(connection, workItemType));
            }

            return new TFSWorkItemTypeCollection(connection, list);
        }
    }
}
